package openGl;

import android.opengl.GLES20;
import android.opengl.GLSurfaceView;

import java.nio.FloatBuffer;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import camera.OpenGl.utils.ByteBufferUtils;

import static android.opengl.Matrix.orthoM;

/**
 *@author:ike
 *@function: 正交矩阵投影
 *@creat time:15:31 2017/12/27
 **/

public class OrthogonalityRender implements GLSurfaceView.Renderer {
    private String Tag="OrthogonalityRender";
    /**
     * 正交矩阵的返回值（必须大于16位）
     */
    private final float[] projectionMatrix = new float[16];
    /**
     * 顶点着色器
     */
    public  final String vertexShaderCode = "uniform mat4 u_Matrix;\n" +
            "\n" +
            "attribute vec4 a_Position;  \n" +
            "attribute vec4 a_Color;\n" +
            "\n" +
            "varying vec4 v_Color;\n" +
            "\n" +
            "void main()                    \n" +
            "{                            \n" +
            "    v_Color = a_Color;\n" +
            "\t  \t  \n" +
            "    gl_Position = u_Matrix * a_Position;\n" +
            "    gl_PointSize = 10.0;          \n" +
            "}         ";
    /**
     * 片元着色器
     */
    public  final String fragmentShaderCode ="precision mediump float; \t\t\t\t\n" +
            "varying vec4 v_Color;      \t   \t\t\t\t\t\t\t\t\n" +
            "  \n" +
            "void main()                    \t\t\n" +
            "{                              \t\n" +
            "    gl_FragColor = v_Color;                                  \t\t\n" +
            "}";
    float[] tableVerticesWithTriangles = {
            // Order of coordinates: X, Y, R, G, B

            // Triangle Fan
            0f,    0f,   1f,   1f,   1f,
            -0.5f, -0.8f, 0.7f, 0.7f, 0.7f,
            0.5f, -0.8f, 0.7f, 0.7f, 0.7f,
            0.5f,  0.8f, 0.7f, 0.7f, 0.7f,
            -0.5f,  0.8f, 0.7f, 0.7f, 0.7f,
            -0.5f, -0.8f, 0.7f, 0.7f, 0.7f,

            // Line 1
            -0.5f, 0f, 1f, 0f, 0f,
            0.5f, 0f, 1f, 0f, 0f,

            // Mallets
            0f, -0.4f, 0f, 0f, 1f,
            0f,  0.4f, 1f, 0f, 0f
    };
    private FloatBuffer trianglesBuffer;
    private int uMatrix;

    public OrthogonalityRender() {
        trianglesBuffer= ByteBufferUtils.makeFloteByteBuffer(tableVerticesWithTriangles);
    }
    @Override
    public void onSurfaceCreated(GL10 gl, EGLConfig config) {
        GLES20.glClearColor(0,0,0,0);
        int program = GLES20.glCreateProgram();
        int i = OpenGlUtils.loadShader(GLES20.GL_VERTEX_SHADER, vertexShaderCode);
        int i1 = OpenGlUtils.loadShader(GLES20.GL_FRAGMENT_SHADER, fragmentShaderCode);
        GLES20.glAttachShader(program,i);
        GLES20.glAttachShader(program,i1);
        GLES20.glLinkProgram(program);

        GLES20.glUseProgram(program);
        int a_position = GLES20.glGetAttribLocation(program, "a_Position");
        int a_color = GLES20.glGetAttribLocation(program, "a_Color");
        uMatrix = GLES20.glGetUniformLocation(program, "u_Matrix");
        trianglesBuffer.position(0);
        GLES20.glEnableVertexAttribArray(a_position);
        GLES20.glVertexAttribPointer(
                a_position,
                2,
                GLES20.GL_FLOAT,
                false,
                5*4,//步长
                trianglesBuffer
        );
        trianglesBuffer.position(2);
        GLES20.glEnableVertexAttribArray(a_color);
        GLES20.glVertexAttribPointer(a_color,3,GLES20.GL_FLOAT,
                false,5*4,trianglesBuffer);
    }

    @Override
    public void onSurfaceChanged(GL10 gl, int width, int height) {
        GLES20.glViewport(0,0,width,height);
        //处理横竖屏切换的问题
        float radio=width>height?width*1.f/height:height*1.0f/width;
        if (width>height){
            //横屏
            //创建正交投影
            orthoM(projectionMatrix,
                    0,-radio,radio,
                    -1f,1f,
                    -1f,1f);
        }else {
            //竖屏
            orthoM(projectionMatrix,0,
                    -1f,1f,
                    -radio,radio,-1,1);
        }
    }

    @Override
    public void onDrawFrame(GL10 gl) {
        GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);
        //将创建好的正交矩阵投影交于着色器使用
        GLES20.glUniformMatrix4fv(uMatrix,1,false,projectionMatrix,0);
        
        GLES20.glDrawArrays(GLES20.GL_TRIANGLE_FAN,0,6);
        GLES20.glDrawArrays(GLES20.GL_LINES,6,2);
    }
}
